﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Century.Web.Security.Authorizations;
using Century.Model;
using Century.EnterpriseLibrary.Globalization;
using Century.Common;
using Century.Model.CMS;
using Century.Business.CMS;
using System.Web.Security;

namespace Century.Web.UI.MVC.Helper
{
    public static class GlobalizationExtensions
    {
        static ISiteService siteService = new SiteService();
       

        #region [ Label(s) ]

        public static IHtmlString Translate(this string defaultValue)
        {
            return Translate(defaultValue, defaultValue);
        }

        public static IHtmlString Translate(this string defaultValue, string key, string category = "")
        {
            string value = LabelValue(defaultValue, key, category);
            return new HtmlString(value);
        }

        public static IHtmlString Label(this string defaultValue)
        {
            return Label(defaultValue, defaultValue);
        }

        public static IHtmlString Label(this string defaultValue, string key, string category = "")
        {
            string value = LabelValue(defaultValue, key, category);

            if (System.Web.Security.Roles.IsUserInRole("Administrator") ||
                   // (siteService.GetSites().Any(s => s.Id == FrameworkToken.UserToken.CurrentSite.Id)) &&
                    CenturyAuthorization.Authorize( new ActionModule { Name = "InlineEditing", DisplayName = "Inline Editing" }) &&
                    (HttpContext.Current.Request.Params["desingmode"] == null || HttpContext.Current.Request.Params["desingmode"] != "true")

               )
           
            {
                value = string.Format("<far class='{3}' key='{0}' category='{1}' countrysiteid='{4}'>{2}</far>"
                        , HttpUtility.HtmlEncode(key)
                        , HttpUtility.HtmlEncode(category)
                        , value
                        , "localizedText"
                        , FrameworkToken.UserToken.CurrentSite.Id);
            }


            return new HtmlString(value);
        }

        private static string LabelValue(string defaultValue, string key, string category)
        {
            if (string.IsNullOrEmpty(key) && string.IsNullOrEmpty(category))
            {
                return defaultValue;
            }

            return GlobalizationManager.CMSTranslate(category, key, defaultValue, FrameworkToken.UserToken.CurrentSite.Id);
        }

        #endregion


        #region [ Position(s) ]

        public static MvcHtmlString Position(this HtmlHelper helper, string name)
        {
            bool isAuthorize = false;
            if (
                Roles.IsUserInRole("Administrator") ||
                CenturyAuthorization.Authorize(new ActionModule { Name = "InlineEditing", DisplayName = "Inline Editing" }) &&
                (HttpContext.Current.Request.Params["draftMode"] != null && HttpContext.Current.Request.Params["draftMode"] == "true")
               )
            {
                isAuthorize = true;
            }
            string pageName = helper.ViewContext.RouteData.Values["pgName"].ToString();
            if (string.IsNullOrEmpty(pageName))
            {
                pageName = "home";
            }

            ContentHolder contentHolder = siteService.GetContentHolder(name, FrameworkToken.UserToken.CurrentSite.Id, pageName);// PersistData.CurrentPage.PageLayout.ContentHolders.First(c => c.Name == name);
            if (contentHolder == null || contentHolder.ContentHolderParts == null)
            {
                return new MvcHtmlString("");
            }
            List<ContentHolderPart> holderParts = contentHolder.ContentHolderParts.ToList();

            string result = string.Empty;
                        if (isAuthorize)
                        {
                            // only admins can see addview button
                            string addButtonsStr = "<a href='#' class='addContent'>Add Content</a>";
                            if (CenturyAuthorization.Authorize(new ActionModule { Name = "Administration", DisplayName = "Administration" }))
                            {
                                addButtonsStr = string.Format("{0}<a href='#' class='addView' >Add View</a>", addButtonsStr);
                            }
                            SitePage currentPage = siteService.GetPage(pageName, FrameworkToken.UserToken.CurrentSite.Id);// PersistData.CurrentPage.PageLayout.ContentHolders.First(c => c.Name == name);

                            result = string.Format("<far class='{2}' key='{0}' pageId='{1}' holderId='{3}' countrysiteid='{5}'>{4}",
                                                    name,
                                                    currentPage.Id,
                                                    "localizedPosition",
                                                    contentHolder.Id,
                                                    addButtonsStr,
                                                    FrameworkToken.UserToken.CurrentSite.Id
                                                  );
                            foreach (var holderPart in holderParts)
                            {

                                if (holderPart.StaticContentId.HasValue)
                                {
                                    result +=
                                  @"<p><div class='ContentHolderHeader'>
                                        <img src='/content/themes/default/img/editor/c_move.jpg' width='10px' height='20px' class='positionContentHolderMove' alt='move'/>
                                        <img src='/content/themes/default/img/editor/c_edit.gif' class='positionContentHolderEdit' alt='Edit'/>
                                        <img src='/content/themes/default/img/editor/c_remove.gif' class='positionContentHolderRemove' alt='remove'/>
                                        <img src='/content/themes/default/img/editor/c_up.gif' class='positionContentHolderToggle' alt='Toggle'/>
                                    </div>
                                    <div style='clear: both;' ></div>";
                                    result += string.Format("<far class='{2}' key='{0}' pageId='{1}' ordid='{3}' holderId='{4}' partId='{5}' countrysiteid='{6}'>", holderPart.StaticContentId.Value,
                                         currentPage.Id, "localizedStaticContent", holderPart.PartOrder, holderPart.ContentHolderId, holderPart.Id, FrameworkToken.UserToken.CurrentSite.Id);
                                    result += holderPart.StaticContent.Description;// +@"<br />";
                                    result += "</far></p>";
                                }
                                else if (holderPart.DynamicContentId.HasValue)
                                {
                                    result +=
                                  @"<p><div class='ContentHolderHeader'>
                                        <img src='/content/themes/default/img/editor/c_move.jpg' width='10px' height='20px' class='positionContentHolderMove' alt='move'/>
                                        <img src='/content/themes/default/img/editor/c_remove.gif' class='positionContentHolderRemove' alt='remove'/>
                                        <img src='/content/themes/default/img/editor/c_up.gif' class='positionContentHolderToggle' alt='Toggle'/>
                                    </div>
                                    <div style='clear: both;' ></div>";
                                    result += string.Format("<far class='{2}' key='{0}' pageId='{1}' ordid='{3}' holderId='{4}' partId='{5}' countrysiteid='{6}'>", holderPart.DynamicContentId.Value,
                                         currentPage.Id, "localizedDynamicContent", holderPart.PartOrder, holderPart.ContentHolderId, holderPart.Id, FrameworkToken.UserToken.CurrentSite.Id);
                                    result += System.Web.Mvc.Html.ChildActionExtensions.Action(helper, holderPart.DynamicContent.ActionName, holderPart.DynamicContent.ControllerName, new { area = holderPart.DynamicContent.AreaName });
                                    result += "</far></p>";

                                }

                            }
                            result += "</far>";
                        }
                        else
                        {
            foreach (var holderPart in holderParts)
            {
                if (holderPart.StaticContentId.HasValue)
                {
                    result += string.Format("<p>{0}</p>", holderPart.StaticContent.Description);
                }
                else if (holderPart.DynamicContentId.HasValue)
                {
                    result += string.Format("<p>{0}</p>", System.Web.Mvc.Html.ChildActionExtensions.Action(helper, holderPart.DynamicContent.ActionName, holderPart.DynamicContent.ControllerName, new { area = holderPart.DynamicContent.AreaName }));
                }

            }
            }
            return new MvcHtmlString(result);
        }

        #endregion

        #region [ Private Method(s) ]


        #endregion
    }
}
